package com.lzy.business.controller.approval;

import com.github.pagehelper.PageInfo;
import com.lzy.business.request.ProfitErrorRequest;
import com.lzy.business.response.ProfitErrorResponse;
import com.lzy.framework.component.PageBean;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.orm.db.Pager;
import com.lzy.framework.core.output.Result;
import com.lzy.framework.core.utils.DozerUtils;
import com.lzy.product.apis.OrderProductServiceApi;
import com.lzy.product.apis.OrderProfitServiceApi;
import com.lzy.product.common.util.EnumUtil;
import com.lzy.product.constants.BusinessType.ProfitErrorStatus;
import com.lzy.product.dtos.OrderProductDto;
import com.lzy.product.dtos.profit.ProfitErrorDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 分利错误记录
 * Created by WIN7 on 2017/12/20.
 */
@Controller
@RequestMapping("/admin/profit/error")
public class ProfitErrorController {
    @Autowired
    private OrderProfitServiceApi orderProfitServiceApi;
    @Autowired
    private OrderProductServiceApi orderProductServiceApi;

    /**
     * 分利错误记录页面
     *
     * @return
     */
    @RequestMapping(value = "profitErrorListPage", method = RequestMethod.GET)
    public String profitErrorListPage(Model model) {
        List<ProfitErrorStatus> statusList = EnumUtil.valueOfList(ProfitErrorStatus.class);
        model.addAttribute("statusList", statusList);
        return "approval/profit_error_list";
    }

    /**
     * 分利错误记录列表   返回json在页面
     */
    @RequestMapping(value = "/json", method = RequestMethod.GET)
    @ResponseBody
    public PageBean profitErrorList(ProfitErrorRequest request) {
        Pager pager = new Pager();
        pager.setPageSize(request.getPageSize());
        pager.setPageNum(request.getPageNum());
        PageInfo<ProfitErrorDto> pageInfo = orderProfitServiceApi.queryErrorList(pager, DozerUtils.map(request, ProfitErrorDto.class));
        if (pageInfo != null) {
            return new PageBean(pageInfo.getTotal(), DozerUtils.maps(pageInfo.getList(), ProfitErrorResponse.class));
        }
        return null;
    }

    /**
     * 处理错误记录   返回json在页面
     */
    @RequestMapping(value = "/dealProfitError/{orderId}", method = RequestMethod.PUT)
    @ResponseBody
    public Result dealProfitError(@PathVariable("orderId") String orderId) {
        OrderProductDto dto = orderProductServiceApi.findById(Long.parseLong(orderId));
        if (dto != null) {
            Ensure.that(orderProductServiceApi.calculateProfit(dto)).isTrue("13020004");
        } else{
            Ensure.that(orderProfitServiceApi.dealProfit(Long.parseLong(orderId), ProfitErrorStatus.DEAL_SUCCESS.getCode())).isTrue("13020004");
        }
        return new Result();
    }
}
